//
//  LRXSubThingView.m
//  路人侠
//
//  Created by zhangxulong on 4/17/16.
//  Copyright © 2016 zhangxulong. All rights reserved.
//

#import "LRXSubThingView.h"
#import "LRXThingStoreCell.h"
#import "LRXBuyThingCell.h"
#import "SVProgressHUD.h"
#import "LRXBuyThingController.h"

static NSString * storeReUsedCell = @"storeReUsedCell";
static NSString * thingReUsedCell = @"thingReUsedCell";

@interface LRXSubThingView()<UITableViewDelegate,UITableViewDataSource,BuyThingControllerReDataDelegate>
@property (nonatomic,strong) UITableView * foodScroll;
@property (nonatomic,strong) UITableView * storeScroll;
@property (nonatomic,strong) NSMutableArray * store;
@property (nonatomic,strong) NSMutableArray * foods;
@property (nonatomic,strong) LRXSingleTon * oneS;
@end

@implementation LRXSubThingView
/**
 *  每一个地点有 商店栏和物品栏
 */

- (void)SubThingView
{
    
    UITableView * foodScroll = [[UITableView alloc]init];
    foodScroll.frame = CGRectMake(160 * wScale, 0, self.frame.size.width - 160 * wScale, self.frame.size.height - 100 * hScale);
    foodScroll.delegate = self;
    foodScroll.dataSource = self;
    foodScroll.rowHeight = 180 * hScale;
    foodScroll.separatorStyle = UITableViewCellSeparatorStyleNone;
    foodScroll.showsVerticalScrollIndicator = NO;
    
    
    self.foodScroll=foodScroll;
    self.foodScroll.tag = 101;
    
    UITableView * storeScroll = [[UITableView alloc]init];
    storeScroll.frame = CGRectMake(0, 0, 160 * wScale, self.frame.size.height);
    storeScroll.contentSize = CGSizeMake(160 * wScale, self.frame.size.height - 100 * hScale);
    storeScroll.delegate = self;
    storeScroll.dataSource = self;
    storeScroll.rowHeight = 88 * hScale;
    storeScroll.separatorStyle = UITableViewCellSeparatorStyleNone;
    storeScroll.backgroundColor = [UIColor colorWithRed:236 green:236 blue:236];
    
    self.storeScroll = storeScroll;
    self.storeScroll.tag = 100;
    
    
    
    [[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(reData) name:@"reLoadData" object:nil];
    
    
    
    
    [self loadSubThingView];
    [self addSubview:self.foodScroll];
    [self addSubview:self.storeScroll];
    
    //刷新 在 contentView 修改后的物品
    //[LRXBuyThingCell setBuyThing:[LRXBuyThingCell getConfirmBuyThing]];
    
    
}
- (void)loadSubThingView
{
    //获取地点的  多个商店
    
    NSDictionary * param = @{@"f":self.placeModel.placeID};
    AFHTTPSessionManager * manger = [AFHTTPSessionManager manager];
    [manger GET:getNavStoreUrl parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            NSMutableArray * store = [NSMutableArray array];
            for (NSDictionary * dic in responseObject[@"data"][@"dataset"]) {
                LRXBuyStoreModel * model = [LRXBuyStoreModel buyStoreWith:dic];
                [store addObject:model];
            }
            
            self.store = store;
            LRXBuyStoreModel * firstStore = self.store[0];
            [self loadThingInfo:firstStore.storeID withPage:@"1"];
            [self.storeScroll reloadData];
            
            NSIndexPath * firsetIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
            [self.storeScroll selectRowAtIndexPath:firsetIndexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
            
            
        });

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [MBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow];
    }];
    
}

- (void)loadThingInfo:(NSString *)storeID withPage:(NSString * )page
{
    
    AFHTTPSessionManager * manger = [AFHTTPSessionManager manager];
    NSDictionary * param = @{@"s":storeID,@"page":@"1"};
    [manger GET:getListURL parameters:param progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
       dispatch_async(dispatch_get_main_queue(), ^{
           
           if ([responseObject[@"data"][@"status_code"] isEqualToString:@"1"]) {
               [MBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow animated:YES];
               [SVProgressHUD dismiss];
               
               NSMutableArray * food = [NSMutableArray array];
               for (NSDictionary * thingDic in  responseObject[@"data"][@"dataset"]) {
                   LRXThing * thing = [LRXThing thingWith:thingDic];
                   [food addObject:thing];
               }
               self.foods = food;
               
               [self.foodScroll reloadData];
               
           }
       });
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
         [MBProgressHUD hideHUDForView:[UIApplication sharedApplication].keyWindow animated:YES];
        [SVProgressHUD dismiss];
    }];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    if (tableView.tag == 100) {
        return self.store.count;
    }
    else if(tableView.tag == 101)
    {
        return self.foods.count;
    }
    else
        return 0;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
    
    if (tableView.tag == 100) {
        static NSString * storeReUsedCell = @"storeReUsedCell";
        LRXThingStoreCell * cell = [tableView dequeueReusableCellWithIdentifier:storeReUsedCell];
        if (cell == nil) {
            cell = [[LRXThingStoreCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:storeReUsedCell];
        }
        LRXBuyStoreModel * store = self.store[indexPath.row];
        [cell layoutThingStoreCell:store.storeName];
        cell.backgroundColor = [UIColor colorWithRed:236 green:236 blue:236];
        return cell;
    }
        
    else if(tableView.tag == 101){
        static NSString * thingReUsedCell = @"thingReUsedCell";
        LRXBuyThingCell * foodCell = [tableView dequeueReusableCellWithIdentifier:thingReUsedCell];
        if (foodCell == nil) {
            
            foodCell = [[LRXBuyThingCell alloc]initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:thingReUsedCell];
            foodCell.thingInfo = self.foods[indexPath.row];
            [foodCell layoutBuyThingCellSubView];
            
        }
        else
        {
            
            foodCell.thingInfo = self.foods[indexPath.row];
            [foodCell setBuyThingCellModel];
  
        }
        
        
        
        foodCell.thingCellDelegate = self.superController;
        foodCell.selectionStyle = UITableViewCellSelectionStyleNone;
        return foodCell;
    }
    else
        return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView.tag == 100) {
        
        [SVProgressHUD showWithStatus:nil];
        
        LRXBuyStoreModel * store = self.store[indexPath.row];
        if ([self.delegate respondsToSelector:@selector(storeCellClickToChangedTitle:)]) {
            NSString * title = [self.store[indexPath.row] storeName];
            [self.delegate storeCellClickToChangedTitle:title];
        }
        [self loadThingInfo:store.storeID withPage:@"1"];
        [self.foodScroll reloadData];
    }
    
}

- (void)reData{
    
    
//    dispatch_async(dispatch_get_main_queue(), ^{
//    self.foodScroll.delegate = self;
//    self.foodScroll.dataSource = self;
    
    //[self.foodScroll reloadData];
    
    dispatch_async(dispatch_get_main_queue(), ^{

        
        [self.foodScroll reloadData];
        
    });

    
//    [self addSubview:_foodScroll];
//    [self.foodScroll reloadData];
//    });
    
}
@end
